package com.leinao.file.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.constant.Constant;
import com.leinao.file.service.FileService;
import com.leinao.login.dto.LoginResultDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 *
 * @Description 文件下载控制类
 * @author lizi
 * @date 2018/10/16 上午11:19
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class FileDownloadController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FileService fileService;

    @Autowired
    private HttpServletRequest request;

    /**
     * @Description 单个文件下载，多文件批量打包下载
     * @author lizi
     * @date 2018/11/19 下午8:33
     *
     * zip压缩和解压：
     * http://www.blogjava.net/liuzheng/articles/176807.html
     * https://blog.csdn.net/yangshangwei/article/details/77913798
     */
    @ApiOperation(value="文件下载", notes="文件下载")
    @GetMapping(value = "/file/download")
    public void fileDownload(@ApiParam(value = "业务类型: 1-文件中心录，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                             @ApiParam(value = "目录名", required = true) @RequestParam String[] dirs,
                             HttpServletResponse response) {
        logger.info("download param type : {}, dirs: {}", type, Arrays.toString(dirs));

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        // 下载文件
        fileService.downLoad(userInfo.getUserId(), type, dirs, response);
    }

    /**
     * @Description 文件预览
     * @author lizi
     * @date 2018/11/22 上午10:33
     */
    @ApiOperation(value="文件预览", notes="文件预览")
    @GetMapping(value = "/file/preview")
    public void previewFile(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                            @ApiParam(value = "文件路径", required = true) @RequestParam String path,
                            HttpServletResponse response) {
        logger.info("previewFile param type : {}, path: {}", type, path);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        // 下载文件
        fileService.previewFile(userInfo.getUserId(), type, path, response);
    }
    /**
     * @Description 公开模型文件预览
     * @author lizi
     * @date 2018/11/22 上午10:33
     */
    @ApiOperation(value="公开模型文件预览", notes="公开模型文件预览")
    @GetMapping(value = "/file/previewForModel")
    public void previewFile(@ApiParam(value = "业务类型: 1-文件中心，2-项目根目录，3-个人数据集和公开数据集", required = true) @RequestParam Integer type,
                            @ApiParam(value = "文件路径", required = true) @RequestParam String path,
                            @ApiParam(value = "模型创建者ID", required = true) @RequestParam String userId,
                            HttpServletResponse response) {
        logger.info("previewFile param type : {}, path: {}", type, path);

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj,LoginResultDto.class );

        // 下载文件
        fileService.previewFile(userId, type, path, response);
    }
}
